﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;

namespace Web.Page
{
    public partial class PermissionEdit : System.Web.UI.Page
    {
        protected readonly string KEY = "_pid_";
        protected string id;
        protected Permission permission;
        protected string resultMessage = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            id = Request.QueryString["id"];

            if (!IsPostBack)
            {
                if (id == null)
                {
                    this.lblTitle.Text = "新增权限";
                }
                else
                {
                    this.lblTitle.Text = "修改权限";
                    Permissions_Dao dao = new Permissions_Dao();
                    permission = dao.Query(id);
                    setFields();
                }
            }
        }

        private void add()
        {
            permission = new Permission()
            {
                Id = Guid.NewGuid(),
            };
            getFields();

            Permissions_Dao dao = new Permissions_Dao();

            try
            {
                int result = dao.Add(permission);
                if (result == 1)
                {
                    resultMessage = "添加权限成功";
                }
            }
            catch (Exception ex)
            {
                resultMessage = "添加权限失败:" + ex.Message;
            }
        }

        private void update()
        {
            permission = new Permission()
            {
                Id = Guid.Parse(id),
            };
            getFields();
            Permissions_Dao dao = new Permissions_Dao();

            try
            {
                int result = dao.Update(permission);
                if (result == 1)
                {
                    resultMessage = "更新权限成功";
                }
            }
            catch (Exception ex)
            {
                resultMessage = "更新权限失败:" + ex.Message;
            }
        }

        private void setFields()
        {
            this.tbCode.Text = permission.Code;
            this.tbName.Text = permission.Name;
            this.tbDescription.Text = permission.Description;
        }

        private void getFields()
        {
            permission.Code = this.tbCode.Text;
            permission.Name = this.tbName.Text; ;
            permission.Description = this.tbDescription.Text;
        }

        private void ClearControlsValue()
        {
            this.tbCode.Text = "";
            this.tbName.Text = "";
            this.tbDescription.Text = "";

            this.lbResult.Text = "";
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            AsyncPostBackTrigger outsideTrigger = new AsyncPostBackTrigger();
            outsideTrigger.ControlID = "btnSave";
            outsideTrigger.EventName = "Click";
            this.UpdatePanel2.Triggers.Add(outsideTrigger);
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            if (id == null)
            {
                add();
            }
            else
            {
                update();
            }

            this.lbResult.Text = resultMessage;
        }

        protected void btnClear_Click(object sender, ImageClickEventArgs e)
        {
            if (id == null)
            {
                ClearControlsValue();
            }
            else
            {
                Permissions_Dao dao = new Permissions_Dao();
                permission = dao.Query(id);
                setFields();
            }
        }

        protected void btnReturn_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("PermissionManage.aspx");
        }
    }
}